home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / DXTex / dxtex.cpp < prev    next >
C/C++ Source or Header  |  2001-10-08  |  10KB  |  374 lines

  1. // dxtex.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "dxtex.h"
  6.  
  7. #include "MainFrm.h"
  8. #include "ChildFrm.h"
  9. #include "dxtexDoc.h"
  10. #include "dxtexView.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18.  
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CDxtexDocManager::DoPromptFileName - overridden to allow importing of
  22. // BMPs as well as DDSs into CDxtexDocs.
  23. BOOL CDxtexDocManager::DoPromptFileName(CString& fileName, UINT nIDSTitle,
  24.             DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate)
  25. {
  26.     CFileDialog dlgFile(bOpenFileDialog);
  27.  
  28.     CString title;
  29.     VERIFY(title.LoadString(nIDSTitle));
  30.  
  31.     dlgFile.m_ofn.Flags |= lFlags;
  32.  
  33.     CString strFilter;
  34.     CString strDefault;
  35.  
  36.     if (bOpenFileDialog)
  37.     {
  38.         strFilter += "Image Files (*.dds, *.bmp, *.tga, *.jpg, *.png, *.dib)";
  39.         strFilter += (TCHAR)'\0';   // next string please
  40.         strFilter += _T("*.dds;*.bmp;*.tga;*.jpg;*.png;*.dib");
  41.         strFilter += (TCHAR)'\0';   // last string
  42.         dlgFile.m_ofn.nMaxCustFilter++;
  43.     }
  44.     else
  45.     {
  46.         strFilter += "Image Files (*.dds)";
  47.         strFilter += (TCHAR)'\0';   // next string please
  48.         strFilter += _T("*.dds");
  49.         strFilter += (TCHAR)'\0';   // last string
  50.         dlgFile.m_ofn.nMaxCustFilter++;
  51.     }
  52.  
  53.     // append the "*.*" all files filter
  54.     CString allFilter;
  55.     VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
  56.     strFilter += allFilter;
  57.     strFilter += (TCHAR)'\0';   // next string please
  58.     strFilter += _T("*.*");
  59.     strFilter += (TCHAR)'\0';   // last string
  60.     dlgFile.m_ofn.nMaxCustFilter++;
  61.  
  62.     dlgFile.m_ofn.lpstrFilter = strFilter;
  63.     dlgFile.m_ofn.lpstrTitle = title;
  64.     dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
  65.  
  66.     INT_PTR nResult = dlgFile.DoModal();
  67.     fileName.ReleaseBuffer();
  68.     return nResult == IDOK;
  69. };
  70.  
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CDxTxCommandLineInfo
  74.  
  75. CDxtexCommandLineInfo::CDxtexCommandLineInfo(VOID)
  76. {
  77.     m_fmt = D3DFMT_UNKNOWN;
  78.     m_bAlphaComing = FALSE;
  79.     m_bMipMap = FALSE;
  80. }
  81.  
  82.  
  83. void CDxtexCommandLineInfo::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)
  84. {
  85.     if (lstrcmpiA(pszParam, "DXT1") == 0)
  86.     {
  87.         m_fmt = D3DFMT_DXT1;
  88.     }
  89.     else if (lstrcmpiA(pszParam, "DXT2") == 0)
  90.     {
  91.         m_fmt = D3DFMT_DXT2;
  92.     }
  93.     else if (lstrcmpiA(pszParam, "DXT3") == 0)
  94.     {
  95.         m_fmt = D3DFMT_DXT3;
  96.     }
  97.     else if (lstrcmpiA(pszParam, "DXT4") == 0)
  98.     {
  99.         m_fmt = D3DFMT_DXT4;
  100.     }
  101.     else if (lstrcmpiA(pszParam, "DXT5") == 0)
  102.     {
  103.         m_fmt = D3DFMT_DXT5;
  104.     }
  105.     else if (bFlag && tolower(pszParam[0]) == 'a')
  106.     {
  107.         m_bAlphaComing = TRUE;
  108.     }
  109.     else if (!bFlag && m_bAlphaComing)
  110.     {
  111.         m_strFileNameAlpha = pszParam;
  112.         m_bAlphaComing = FALSE;
  113.     }
  114.     else if (bFlag && tolower(pszParam[0]) == 'm')
  115.     {
  116.         m_bMipMap = TRUE;
  117.     }
  118.     else if (!bFlag && !m_strFileName.IsEmpty())
  119.     {
  120.         m_strFileNameSave = pszParam;
  121.     }
  122.  
  123.     CCommandLineInfo::ParseParam(pszParam, bFlag, bLast);
  124. }
  125.  
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // CDxtexApp
  130.  
  131. BEGIN_MESSAGE_MAP(CDxtexApp, CWinApp)
  132.     //{{AFX_MSG_MAP(CDxtexApp)
  133.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  134.         // NOTE - the ClassWizard will add and remove mapping macros here.
  135.         //    DO NOT EDIT what you see in these blocks of generated code!
  136.     //}}AFX_MSG_MAP
  137.     // Standard file based document commands
  138.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  139.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  140. END_MESSAGE_MAP()
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // CDxtexApp construction
  144.  
  145. CDxtexApp::CDxtexApp()
  146. {
  147.     // Place all significant initialization in InitInstance
  148.     m_pd3d = NULL;
  149. }
  150.  
  151. /////////////////////////////////////////////////////////////////////////////
  152. // CDxtexApp destruction
  153.  
  154. CDxtexApp::~CDxtexApp()
  155. {
  156.     ReleasePpo(&m_pd3ddev);
  157.     ReleasePpo(&m_pd3d);
  158. }
  159.  
  160. /////////////////////////////////////////////////////////////////////////////
  161. // The one and only CDxtexApp object
  162.  
  163. CDxtexApp theApp;
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CDxtexApp initialization
  167.  
  168. BOOL CDxtexApp::InitInstance()
  169. {
  170.     // Change the registry key under which our settings are stored.
  171.     SetRegistryKey(_T("Microsoft"));
  172.  
  173.     LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  174.  
  175.     // Register the application's document templates.  Document templates
  176.     //  serve as the connection between documents, frame windows and views.
  177.  
  178.     m_pDocManager = new CDxtexDocManager;
  179.     
  180.     CMultiDocTemplate* pDocTemplate;
  181.     pDocTemplate = new CMultiDocTemplate(
  182.         IDR_DXTXTYPE,
  183.         RUNTIME_CLASS(CDxtexDoc),
  184.         RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  185.         RUNTIME_CLASS(CDxtexView));
  186.     AddDocTemplate(pDocTemplate);
  187.  
  188.     // create main MDI Frame window
  189.     CMainFrame* pMainFrame = new CMainFrame;
  190.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  191.         return FALSE;
  192.     m_pMainWnd = pMainFrame;
  193.  
  194.     // Initialize DirectDraw
  195.     m_pd3d = Direct3DCreate8(D3D_SDK_VERSION);
  196.     if (m_pd3d == NULL)
  197.     {
  198.         AfxMessageBox(ID_ERROR_D3DCREATEFAILED, MB_OK, 0);
  199.         return FALSE;
  200.     }
  201.  
  202.     HRESULT hr;
  203.     D3DDISPLAYMODE dispMode;
  204.     D3DPRESENT_PARAMETERS presentParams;
  205.     D3DDEVTYPE devType;
  206.  
  207.     m_pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dispMode);
  208.  
  209.     ZeroMemory(&presentParams, sizeof(presentParams));
  210.     presentParams.Windowed = TRUE;
  211.     presentParams.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
  212.     presentParams.BackBufferWidth = 8;
  213.     presentParams.BackBufferHeight = 8;
  214.     presentParams.BackBufferFormat = dispMode.Format;
  215.  
  216.     devType = D3DDEVTYPE_REF; 
  217.  
  218.     hr = m_pd3d->CreateDevice(D3DADAPTER_DEFAULT, devType, m_pMainWnd->GetSafeHwnd(), 
  219.         D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParams, &m_pd3ddev);
  220.     if (FAILED(hr))
  221.     {
  222.         AfxMessageBox(ID_ERROR_CANTCREATEDEVICE);
  223.         return FALSE;
  224.     }
  225.  
  226.     D3DCAPS8 Caps;
  227.     m_pd3ddev->GetDeviceCaps(&Caps);
  228.     if (Caps.PrimitiveMiscCaps & D3DPMISCCAPS_NULLREFERENCE)
  229.     {
  230.         AfxMessageBox(ID_ERROR_NULLREF);
  231.     }
  232.  
  233.     // Parse command line for standard shell commands, DDE, file open
  234.     CDxtexCommandLineInfo cmdInfo;
  235.     ParseCommandLine(cmdInfo);
  236.     // Prevent automatic "New" at startup:
  237.     if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew)
  238.         cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
  239.  
  240.     // Dispatch commands specified on the command line
  241.     if (!ProcessShellCommand(cmdInfo))
  242.         return FALSE;
  243.  
  244.     // See if we loaded a document
  245.     POSITION posTemp = GetFirstDocTemplatePosition();
  246.     CDxtexDoc* pdoc = NULL;
  247.     POSITION pos = pDocTemplate->GetFirstDocPosition();
  248.     if (pos != NULL)
  249.         pdoc = (CDxtexDoc*)pDocTemplate->GetNextDoc(pos);
  250.  
  251.     if (!cmdInfo.m_strFileNameAlpha.IsEmpty())
  252.     {
  253.         if (pdoc != NULL)
  254.         {
  255.             pdoc->LoadAlphaBmp(cmdInfo.m_strFileNameAlpha);
  256.         }
  257.     }
  258.     if (cmdInfo.m_bMipMap)
  259.     {
  260.         if (pdoc != NULL)
  261.         {
  262.             pdoc->GenerateMipMaps();
  263.         }
  264.     }
  265.     if (cmdInfo.m_fmt != 0)
  266.     {
  267.         if (pdoc != NULL)
  268.         {
  269.             pdoc->Compress(cmdInfo.m_fmt, TRUE);
  270.         }
  271.     }
  272.     if (!cmdInfo.m_strFileNameSave.IsEmpty())
  273.     {
  274.         if (pdoc != NULL)
  275.         {
  276.             pdoc->OnSaveDocument(cmdInfo.m_strFileNameSave);
  277.         }
  278.         return FALSE; // Prevent UI from coming up
  279.     }
  280.  
  281.     // The main window has been initialized, so show and update it.
  282.     pMainFrame->ShowWindow(m_nCmdShow);
  283.     pMainFrame->UpdateWindow();
  284.  
  285.     return TRUE;
  286. }
  287.  
  288.  
  289. /////////////////////////////////////////////////////////////////////////////
  290. // CAboutDlg dialog used for App About
  291.  
  292. class CAboutDlg : public CDialog
  293. {
  294. public:
  295.     CAboutDlg();
  296.  
  297. // Dialog Data
  298.     //{{AFX_DATA(CAboutDlg)
  299.     enum { IDD = IDD_ABOUTBOX };
  300.     CString m_strVersion;
  301.     //}}AFX_DATA
  302.  
  303.     // ClassWizard generated virtual function overrides
  304.     //{{AFX_VIRTUAL(CAboutDlg)
  305.     protected:
  306.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  307.     //}}AFX_VIRTUAL
  308.  
  309. // Implementation
  310. protected:
  311.     //{{AFX_MSG(CAboutDlg)
  312.         // No message handlers
  313.     //}}AFX_MSG
  314.     DECLARE_MESSAGE_MAP()
  315. };
  316.  
  317. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  318. {
  319.     TCHAR szFile[MAX_PATH];
  320.     CString strVersion;
  321.     UINT cb;
  322.     DWORD dwHandle;
  323.     BYTE FileVersionBuffer[1024];
  324.     VS_FIXEDFILEINFO* pVersion = NULL;
  325.  
  326.     GetModuleFileName(NULL, szFile, MAX_PATH);
  327.  
  328.     cb = GetFileVersionInfoSize(szFile, &dwHandle/*ignored*/);
  329.     if (cb > 0)
  330.     {
  331.         if (cb > sizeof(FileVersionBuffer))
  332.             cb = sizeof(FileVersionBuffer);
  333.  
  334.         if (GetFileVersionInfo(szFile, 0, cb, &FileVersionBuffer))
  335.         {
  336.             pVersion = NULL;
  337.             if (VerQueryValue(&FileVersionBuffer, "\\", (VOID**)&pVersion, &cb)
  338.                 && pVersion != NULL) 
  339.             {
  340.                 strVersion.Format("Version %d.%02d.%02d.%04d", 
  341.                     HIWORD(pVersion->dwFileVersionMS),
  342.                     LOWORD(pVersion->dwFileVersionMS), 
  343.                     HIWORD(pVersion->dwFileVersionLS), 
  344.                     LOWORD(pVersion->dwFileVersionLS));
  345.             }
  346.         }
  347.     }
  348.  
  349.     //{{AFX_DATA_INIT(CAboutDlg)
  350.     m_strVersion = strVersion;
  351.     //}}AFX_DATA_INIT
  352. }
  353.  
  354. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  355. {
  356.     CDialog::DoDataExchange(pDX);
  357.     //{{AFX_DATA_MAP(CAboutDlg)
  358.     DDX_Text(pDX, IDC_VERSION, m_strVersion);
  359.     //}}AFX_DATA_MAP
  360. }
  361.  
  362. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  363.     //{{AFX_MSG_MAP(CAboutDlg)
  364.         // No message handlers
  365.     //}}AFX_MSG_MAP
  366. END_MESSAGE_MAP()
  367.  
  368. // App command to run the dialog
  369. void CDxtexApp::OnAppAbout()
  370. {
  371.     CAboutDlg aboutDlg;
  372.     aboutDlg.DoModal();
  373. }
  374.